from flask import Flask
from flask_sqlalchemy import SQLAlchemy

from config import *


# 配置基本的web服务信息
app = Flask(__name__, static_url_path='')
app.secret_key = WEB_SECRET_KEY
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + SQLITE3_PATH
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['PERMANENT_SESSION_LIFETIME'] = SESSION_TIME
db = SQLAlchemy(app)


# 跨域设置
if ALLOW_CROSS_DOMAIN:
    from flask_cors import CORS
    CORS(app, supports_credentials=True)


@app.after_request
def after_request(response):
    if ALLOW_CROSS_DOMAIN:
        response.headers['Access-Control-Allow-Origin'] = 'http://localhost'
        response.headers["Access-Control-Allow-Headers"] = "Content-Type"
        response.headers['Access-Control-Allow-Credentials'] = 'true'
    return response


# gzip压缩设置
if GZIP:
    from flask_compress import Compress
    compress = Compress()
    compress.init_app(app)
    app.config['COMPRESS_REGISTER'] = True
    app.config['COMPRESS_MIN_SIZE'] = 50


# 导入其他模块
from script.api.tag import *
from script.api.user import *
from script.api.render import *


# 主函数
if __name__ == '__main__':
    # 自动建表
    with app.app_context():
        db.create_all()

    app.run(host="0.0.0.0", debug=FLASK_DEBUG, port=WEB_PORT)

